package com.pfs.internal.timestamp.reports;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Map;

import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;

import com.pfs.internal.timestamp.entities.TrackDay;
import com.pfs.internal.timestamp.utils.Formater;

public class TrackReportExcel extends FatherExcel<TrackDay> {
	private List<TrackDay> dtls;

	public TrackReportExcel() {
		setCidx(0);
		setRidx(6);
	}

	@Override
	public int layoutDtl(int ri, int ci, List<TrackDay> dtls, Sheet sheet) {
		int stt = 0;
		int rIndex = ri, cIndex = ci;
		String un = "";
		int userSize = 0;
		Calendar calendar = Calendar.getInstance();
		for (TrackDay trackDay : dtls) {
			String username = trackDay.getUsername();
			if (un.equalsIgnoreCase(username)) {
				username = "";
			} else {
				userSize++;
			}
			// if (userSize % 2 == 0) {
			// cs_center_10.setFillForegroundColor(IndexedColors.ORANGE.getIndex());
			// cs_center_10.setFillPattern(CellStyle.SOLID_FOREGROUND);
			// } else {
			// cs_center_10.setFillBackgroundColor(IndexedColors.BRIGHT_GREEN.getIndex());
			// cs_center_10.setFillPattern(CellStyle.SOLID_FOREGROUND);
			// }
			Row row = sheet.createRow(rIndex++);
			ReportUtil.createCell(row, (short) (cIndex), cs_center_10, ++stt);
			ReportUtil.createCell(row, (short) (cIndex + 1), cs_center_10, username);
			ReportUtil.createCell(row, (short) (cIndex + 2), cs_center_10, username);
			ReportUtil.createCell(row, (short) (cIndex + 3), cs_center_10, Formater.date2str(trackDay.getDate()));
			StringBuilder timeIn = new StringBuilder(), timeOut = new StringBuilder();
			int hi, ho, mi, mo;
			if (trackDay.getCheckIn() != null) {
				calendar.setTime(trackDay.getCheckIn());
				hi = calendar.get(Calendar.HOUR_OF_DAY);
				mi = calendar.get(Calendar.MINUTE);
				timeIn.append(hi).append(":").append(mi);
			}
			if (trackDay.getCheckOut() != null) {
				calendar.setTime(trackDay.getCheckOut());
				ho = calendar.get(Calendar.HOUR_OF_DAY);
				mo = calendar.get(Calendar.MINUTE);
				timeOut.append(ho).append(":").append(mo);
			}
			ReportUtil.createCell(row, (short) (cIndex + 4), cs_center_10, timeIn.toString());
			ReportUtil.createCell(row, (short) (cIndex + 5), cs_center_10, timeOut.toString());
			ReportUtil.createCell(row, (short) (cIndex + 6), cs_center_10, trackDay.getNote());
			ReportUtil.createCell(row, (short) (cIndex + 7), cs_center_10, trackDay.getOfficeTimeStr());
			un = trackDay.getUsername();
		}
		return rIndex;
	}

	@Override
	public int layoutCustom(Sheet sheet, int r) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public ArrayList<Cell> getDataHdr(Map<String, Object> search) throws Exception {
		ArrayList<Cell> arr = new ArrayList<Cell>();
		Cell cell = new Cell();
		cell.setKey("{1}");
		cell.setValue("Tháng ");
		cell.setCs(cs_left_10b);
		arr.add(cell);

		return arr;
	}

	@Override
	public void initCellStyles() {
		// TODO Auto-generated method stub

	}

	@Override
	public List<TrackDay> getDataDtls() {
		return this.dtls;
	}

	public void setDtls(List<TrackDay> dtls) {
		this.dtls = dtls;
	}

}
